package tw.com.diary.core;

import java.util.List;

import tw.com.diary.ae.com.DirayBussinessException;
import tw.com.diary.ae.com.Msg;
import tw.com.diary.api.domain.RecViewDTO;
import tw.com.diary.api.domain.RecViewResultDTO;
import tw.com.diary.api.service.RecViewService;
import tw.com.diary.dao.X0001BackDAO;
import tw.com.diary.dao.X0001DAO;
import tw.com.diary.db.domain.X0001;
import tw.com.diary.db.domain.X0001Back;
import tw.com.diary.db.util.DBHelp;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

public class RecViewServiceImpl implements RecViewService {

	private static final String TAG = RecViewServiceImpl.class.getCanonicalName();

	@Override
	public void initQueryX0001s(RecViewDTO dto, Context context) throws DirayBussinessException {

		final DBHelp help = DBHelp.intance(context);

		final SQLiteDatabase con = help.getReadableDatabase();

		try {

			final X0001DAO dao = new X0001DAO(con);

			final List<X0001> x0001s = dao.selectWithlimit();
			dto.getResults().clear();
			for (X0001 x0001 : x0001s) {
				final RecViewResultDTO recViewResultDTO = new RecViewResultDTO();
				recViewResultDTO.setClicked(false);
				recViewResultDTO.setX001(x0001);
				dto.getResults().add(recViewResultDTO);
			}

		} catch (Exception e) {
			throw new DirayBussinessException(Msg.E0001, e.getMessage());
		} finally {
			con.close();
		}

		// TODO Auto-generated method stub

	}

	@Override
	public void removeX0001s(RecViewDTO dto, Context context) throws DirayBussinessException {
		final DBHelp help = DBHelp.intance(context);

		final SQLiteDatabase con = help.getWritableDatabase();

		try {

			final X0001DAO dao = new X0001DAO(con);

			// ====transaction begin======
			con.beginTransaction();

			// 刪除被選擇資料
			for (RecViewResultDTO result : dto.getResults()) {
				if (result.isClicked()) {
					dao.deleteX001WhereQuery(result.getX001().getUuid());
				}
			}

			// 查詢剩餘資料
			final List<X0001> x0001s = dao.selectWithlimit();
			dto.getResults().clear();
			for (X0001 x0001 : x0001s) {
				final RecViewResultDTO recViewResultDTO = new RecViewResultDTO();
				recViewResultDTO.setClicked(false);
				recViewResultDTO.setX001(x0001);
				dto.getResults().add(recViewResultDTO);
			}
			// ====transaction sucessfully======
			con.setTransactionSuccessful();

		} catch (Exception e) {
			Log.e(TAG, "e:" + e);
		} finally {
			con.endTransaction();
			con.close();
		}

		// ====transaction end======//

	}

	@Override
	public void search(RecViewDTO dto, Context context) throws DirayBussinessException {
		final DBHelp help = DBHelp.intance(context);
		final SQLiteDatabase con = help.getReadableDatabase();

		try {

			final X0001DAO dao = new X0001DAO(con);

			final List<X0001> x0001s = dao.selectByContent(dto.getQuery());
			dto.getResults().clear();
			for (X0001 x0001 : x0001s) {
				final RecViewResultDTO recViewResultDTO = new RecViewResultDTO();
				recViewResultDTO.setClicked(false);
				recViewResultDTO.setX001(x0001);
				dto.getResults().add(recViewResultDTO);
			}

		} catch (Exception e) {
			e.printStackTrace();
		} finally {

		}

	}

	@Override
	public void backUpX001(RecViewDTO dto, Context context) throws DirayBussinessException {

		final DBHelp help = DBHelp.intance(context);

		SQLiteDatabase conn = help.getWritableDatabase();

		X0001BackDAO x0001BackDAO = new X0001BackDAO(conn);
		X0001DAO x0001DAO = new X0001DAO(conn);

		try {
			conn.beginTransaction();

			for (RecViewResultDTO result : dto.getResults()) {
				if (result.isClicked()) {
					x0001DAO.deleteX001WhereQuery(result.getX001().getUuid());
					x0001BackDAO.copyToX001Back(result.getX001());
				}
			}

			final List<X0001> x0001s = x0001DAO.selectWithlimit();
			dto.getResults().clear();
			for (X0001 x0001 : x0001s) {
				final RecViewResultDTO recViewResultDTO = new RecViewResultDTO();
				recViewResultDTO.setClicked(false);
				recViewResultDTO.setX001(x0001);
				dto.getResults().add(recViewResultDTO);
			}

			conn.setTransactionSuccessful();
		} catch (Exception e) {

		} finally {
			conn.endTransaction();
			conn.close();
		}
		// TODO Auto-generated method stub

	}
}
